<?php include 'header.php' ?>
<?php
include 'mySQLConnect.php';

$id = $_GET['id'];
$sql3 = "SELECT p.id as 'id_product', c.id as 'id_cat', c.cat_name, p.name, p.price, " .
    "p.ime_product, p.short_desc, p.description, p.image_url FROM  product p" .
    " join category c on p.cat_id = c.id WHERE p.id = '$id'";

$result3 = mysql_query($sql3);

$cat_id = "";
$cat_name = "";
$price = "";
$image_url = "";
$ime = "";
$short_desc = "";
$description = "";
$name = "";

while ($row3 = mysql_fetch_array($result3)) {
    $image_url = $row3['image_url'];

    if (strpos($image_url, '../') !== false) {
        $image_url = substr($image_url, 3);
    }

    $pri = $row3['price'];
    $price = ($row3['price']);
    $temp = "";
    $price = strrev($price);
    while (strlen($price) >= 4) {
        $temp = $temp . substr($price, 0, 3) . '.';
        $price = substr($price, 3);
    }

    if (strlen($price) == 0) {
        $price = strrev($temp);
    } else {
        $price = strrev($price) . strrev($temp);
    }

    $cat_name = urldecode($row3['cat_name']);
    $ime = $row3['ime_product'];
    $short_desc = urldecode($row3['short_desc']);
    $description = urldecode($row3['description']);
    $name = urldecode($row3['name']);
    $cat_id = $row3['id_cat'];
}

include 'mySQLClose.php';
?>
<div class="body">
<?php include 'rightMenu.php' ?>
    <div class="content">
        <div class="products">
            <div class="paging">
                <div class="first">
                    <h2><?php echo $name ?></h2>
                </div>
            </div>

            <div class="product">
                <div class="images">
                    <a id="prettyLink" href="<?php echo $image_url ?>" rel="prettyPhoto">
                        <img alt="" src="<?php echo $image_url ?>"></a>
                </div>
                <div class="summary">
                    <h2 class="product_title"><?php echo $name ?></h2>

                    <div class="attributes">
                        <table>
                            <tr>
                                <td><span class="label">Mã sp: </span></td>
                                <td><?php echo $ime ?></td>
                            </tr>

                            <tr>
                                <td><span class="label">Giá: </span></td>
                                <td><?php echo $price ?> vnđ</td>
                            </tr>

                            <tr>
                                <td><span class="label">Mô tả ngắn gọn: </span></td>
                                <td></td>
                            </tr>

                            <tr>
                                <td colspan="2"><?php echo $short_desc ?></td>
                            </tr>

                            <tr>
                                <td colspan="2"><a
                                        href="javascript:addCart('<?php echo $ime ?>','<?php echo $name ?>','<?php echo $pri ?>')">
                                        <img src="images/add_to_cart.png">
                                    </a></td>
                            </tr>
                            <tr>
                                <td colspan="2"><p id="muahang" style="color: #008000"></p></td>
                            </tr>
                        </table>
                    </div>
                </div>

            </div>

            <div class="productdetail">
                <div><h3>Mô tả chi tiết</h3>

                    <div id="tab-description" class="panel" style="display: block;">
                        <?php echo $description ?>
                    </div>
                    <div id="tab-reviews" class="panel">
                        <div id="reviews">
                            <div class="fb-comments"
                                 data-href="http://localhost/shopquanao"
                                 data-width="100%" data-num-posts="10"></div>
                            <div class="clear"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="products">
            <div class="paging">
                <div class="first">
                    <h2>Sản phẩm liên quan</h2>
                </div>
            </div>
            <ul>
                <?php include 'mySQLConnect.php';

                $sql3 = "SELECT * FROM product WHERE id <> '$id' AND cat_id = '$cat_id' ORDER BY date DESC LIMIT 0,4";

                $result3 = mysql_query($sql3);
                $count = 0;
                while ($row3 = mysql_fetch_array($result3)) {
                    $count++;
                    $image_url = $row3['image_url'];

                    if (strpos($image_url, '../') !== false) {
                        $image_url = substr($image_url, 3);
                    }

                    $price = ($row3['price']);
                    if (strlen($price) > 4) {
                        $price = strrev($price);
                        $price = strrev(substr($price, 3)) . "." . "000";
                    }

                    if ($count % 4 == 1) {
                        ?>
                        <li class="first">
                            <a href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img
                                    src="<?php echo $image_url; ?>"
                                    alt=""/></a>
                            <h4><?php echo urldecode($row3['name']) ?></h4>

                            <p><?php echo urldecode($row3['short_desc']) ?></p>
                            <span><?php echo $price ?> vnđ</span>
                        </li>
                    <?php } else { ?>
                        <li>
                            <a href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img
                                    src="<?php echo $image_url; ?>"
                                    alt=""/></a>
                            <h4><?php echo urldecode($row3['name']) ?></h4>

                            <p><?php echo urldecode($row3['short_desc']) ?></p>
                            <span><?php echo $price ?> vnđ</span>
                        </li>
                    <?php
                    }
                }
                include 'mySQLClose.php';?>
            </ul>
        </div>
    </div>
<?php include 'footer.php' ?>